package im.elephas.androfan.activity;

import im.elephas.androfan.R;
import im.elephas.androfan.fanfouapi.FanfouApi;
import im.elephas.androfan.fanfouapi.events.IChangeEvent;
import im.elephas.androfan.fanfouapi.events.IErrorEvent;
import im.elephas.androfan.fanfouapi.events.IStatusesEvent;
import im.elephas.androfan.fanfouapi.items.StatusItem;
import im.elephas.androfan.service.AndrofanService;
import im.elephas.androfan.util.DateFormater;
import im.elephas.androfan.util.ImageGetter;
import im.elephas.androfan.util.TagFilter;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class FriendsStatusActivity extends StatusActivity {

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(1, 1, 1, "发送").setIcon(android.R.drawable.ic_menu_edit);
		menu.add(1, 2, 2, "回复我的").setIcon(android.R.drawable.ic_menu_call);
		menu.add(1, 3, 3, "空间").setIcon(android.R.drawable.ic_menu_myplaces);
		menu.add(2, 4, 4, "私信").setIcon(android.R.drawable.ic_menu_compass);
		menu.add(2, 5, 5, "随便看看").setIcon(android.R.drawable.ic_menu_gallery);

		menu.add(3, 6, 6, "收藏");
		menu.add(3, 7, 7, "关注我的");
		menu.add(3, 8, 8, "我关注的");
		menu.add(3, 9, 9, "设置");
		menu.add(3, 10, 10, "关于");
		menu.add(3, 11, 11, "退出");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			confirmFinish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i;
		switch (item.getItemId()) {
		case 1:
			i = new Intent(this, SendMessageActivity.class);
			startActivityForResult(i, 0);
			break;
		case 2:
			i = new Intent(getActivity(), ReplyStatusActivity.class);
			startActivityForResult(i, 0);
			break;
		case 3:
			i = new Intent(getActivity(), SpaceStatusActivity.class);
			startActivityForResult(i, 0);
			break;
		case 4:
			break;
		case 5:
			i = new Intent(getActivity(), PublicStatusActivity.class);
			startActivityForResult(i, 0);
			break;
		case 6:
			i = new Intent(getActivity(), FavoritesActivity.class);
			startActivityForResult(i, 0);
			break;
		case 7:
			i = new Intent(getActivity(), FollowersActivity.class);
			startActivityForResult(i, 0);
			break;
		case 8:
			i = new Intent(getActivity(), FriendsActivity.class);
			startActivityForResult(i, 0);
			break;
		case 9:
			break;
		case 10:
			aboutDialog();
			break;
		case 11:
			confirmFinish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void confirmFinish() {
		Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("确定退出?");
		dialog.setMessage("确定退出AndroFan?");
		dialog.setPositiveButton("退出", new Dialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent i = new Intent(getActivity(), AndrofanService.class);
				stopService(i);
				finish();
			}
		});
		dialog.setNegativeButton("取消", null);
		dialog.show();
	}

	private void aboutDialog() {
		Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("关于AnfroFan");
		dialog.setMessage("AndroFan是一款Android平台下的饭否客户端，你可以联系作者@iEle 提供改进建议，也可以参与到开发中来。\n"
				+ "项目主页：http://androfan.googlecode.com/\n"
				+ "作者饭否：http://fanfou.com/elephas");
		dialog.setPositiveButton("提出建议", new Dialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent i = new Intent(getActivity(), SendMessageActivity.class);
				Bundle b = new Bundle();
				b.putBoolean("isreply", true);
				b.putString("atname", "iEle");
				b.putString("replyid", "elephas");
				i.putExtras(b);
				getActivity().startActivityForResult(i, 0);
			}
		});
		dialog.setNegativeButton("取消", null);
		dialog.show();
	}

	@Override
	protected void getStatuses() {
		getFanfouApi().getFriendsTimeline(count, mIStatusesEvent,
				getApiCaller().mIErrorEvent, "refresh");
	}

	@Override
	protected void getMoreStatuses() {
		getFanfouApi().getFriendsTimeline(count, mIStatusesEvent,
				getApiCaller().mIErrorEvent, "more");

	}

	@Override
	protected void setTitle() {
		setTitle(getFanfouApi().getUser().getScreenName() + " 大家的消息");
	}
}
